//
//  StandListViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/19.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation



class StandListViewController: BaseViewController {
    
    var dataList:[UserInfo]         = []
    var viewList:[TCVideoView]    = []
    
    let kStandWidth:CGFloat = 240
    let kStandHeight:CGFloat = 160
    
    override func loadView() {
        let height = CGFloat(Td.sh().usr.remote.count)*kStandHeight+40
        let view = NSView.init(frame: NSRect.init(x: 0, y: 0, width: 240, height: height))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.black.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.loadData()
        self.setupUI()
    }
    override func viewDidAppear() {
        super.viewDidAppear()
        NotificationCenter.default.addObserver(self, selector: #selector(addUser(not:)), name: MTAddUsersNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(deleteUser(not:)), name: MTDeleteUsersNotification, object: nil)
    }
    
    func loadData() {
        let list = Td.sh().usr.remote
        self.dataList = list
    }
    
    func setupUI() {
        self.view.layer?.backgroundColor = color_dark60.cgColor
        
        self.view.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(40, 0, 0, 0))
        }
        
        self.viewList = self.initView(inView: container)
        self.reloadUI(viewList: self.viewList)
        self.reloadData()
    }
    
    lazy var container: NSView = {
        let view = NSView()
        return view
    }()
    
    func initView(inView:NSView) -> [TCVideoView] {
        var viewList:[TCVideoView] = []
        for (_,val) in self.dataList.enumerated() {
            let view = TCVideoView.init(data: val, delegate: nil)
            inView.addSubview(view)
            viewList.append(view)
        }
        return viewList
    }
    
    func reloadUI(viewList:[TCVideoView]) {
        for (inx,val) in viewList.enumerated() {
            val.snp.remakeConstraints({ (make) in
                make.width.equalTo(kStandWidth)
                make.height.equalTo(kStandHeight)
                make.centerX.equalToSuperview()
                if inx == 0 {
                    make.top.equalTo(0)
                }else {
                    make.top.equalTo(viewList[inx-1].snp.bottom)
                }
            })
        }
    }
    
    func reloadDataPart(subscrpts:[Int]) {
        for (_,val) in subscrpts.enumerated() {
            guard let userid = self.dataList[val].userId else { return}
            let videView = self.viewList[val].videoView
            TXLiteManager.shared().startRemoteView(userId: userid, view: videView)
        }
    }
    
    func reloadData() {
        for (inx,val) in self.viewList.enumerated() {
            guard let userid = self.dataList[inx].userId else { return}
            TXLiteManager.shared().startRemoteView(userId: userid, view: val.videoView)
        }
    }
    
    func add(inView:NSView, info:UserInfo) {
        self.dataList.append(info)
        let view = TCVideoView.init(data: info, delegate: nil)
        inView.addSubview(view)
        self.viewList.append(view)
        self.reloadUI(viewList: self.viewList)
        self.reloadDataPart(subscrpts: [self.dataList.count-1])
        self.resWindowFrame()
    }
    
    func delete(inView:NSView, info:UserInfo) {
        let index = self.dataList.firstIndex { (uInfo) -> Bool in
            return info.userId==uInfo.userId
        }
        
        if let inx = index {
            self.viewList[inx].removeFromSuperview()
            self.viewList.remove(at: inx)
            self.dataList.remove(at: inx)
            self.reloadUI(viewList: self.viewList)
            self.resWindowFrame()
        }
    }
    
    func resWindowFrame() {
        let height = CGFloat(self.dataList.count)*kStandHeight+40
        self.view.window?.setContentSize(NSSize.init(width: kStandWidth, height: height))
        
    }
}

extension StandListViewController {
    @objc func addUser(not:Notification) {
        guard let info = not.object as? UserInfo else {
            return
        }
        self.add(inView: self.container, info: info)
    }
    
    @objc func deleteUser(not:Notification) {
        guard let info = not.object as? UserInfo else {
            return
        }
        self.delete(inView: self.container, info: info)
    }
}
